package com.xcity.game.activity2.share.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Player;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.activity2.share.ShareAction;
import com.xcity.game.activity2.share.ShareException;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.item.ItemService;
import com.xcity.pomelo.push.msg.BatchItemsPush;
import com.xcity.pomelo.request.Share2Request;
import com.xcity.pomelo.request.ShareGameRequest;
import com.xcity.pomelo.response.Share2Response;
import com.xcity.util.HttpUtils;

import naga.x.game.time.TimeUpdater;
import naga.x.net.Session;
import naga.x.util.StringUtils;

/**
 * @see AccountChannel#SINA
 * @author yang.li
 *
 */
public class WeiboShareAction implements ShareAction {

	public static final int STONE_REWARD = 2;
	
	@Override
	public Share2Response share(Player player, Share2Request req) throws ShareException {
		String[] params = req.params;
		if (params == null || params.length < 2) {
			throw new ShareException(MessageConst.SHARE_EMPTY);
		}
		String token = params[0];
		String status = params[1];
		String rip = params.length < 3 || StringUtils.isEmpty(params[2]) ? player.getSession().getRemoteAddress() : params[2];
		String error = doShare(player, token, status, rip);
		if (error != null) {
			throw new ShareException(error);
		}
		BatchItemsPush sync = new BatchItemsPush(ItemService.stoneDefault, STONE_REWARD);
		player.send(sync);
		return new Share2Response(req.getSerial(), req.type, req.id, "", 0, STONE_REWARD, null, ShareAction.STATE_REWARDED, 0, 0);
	}

	@Override
	public Share2Response reward(Player player, Share2Request req) throws ShareException {
		throw new ShareException(MessageConst.ILLEGAL_OPERATION);
	}
	
	public static String doShare(Player player, String token, String status, String rip) {
		int lastShareDay = player.getPool().getIntValue(PlayerPool.PROPERTY_LAST_SHARE_GAME_DAY);
		if (lastShareDay == TimeUpdater.getInstance().today()) {
			return MessageConst.SHARED_TODAY;
		}
		AccountChannel channel = player.getAccount().getChannel();
		if (channel != AccountChannel.SINA) {
			return MessageConst.SHARE_NOT_ALLOWED;
		}
		String url = ShareGameRequest.URL_WEIBO;
		if (StringUtils.isEmpty(rip)) {
			Session session = player.getSession();
			rip = session != null ? session.getRemoteAddress() : "";
		}
		String res = HttpUtils.post(url, new String[] {
				"access_token", token,
				"status", status,
				"rip", rip
		});
		JSONObject json = JSON.parseObject(res);
		if (json.getString("error_code") != null) {
			return json.getString("error");
		}
		player.poolSet(PlayerPool.PROPERTY_LAST_SHARE_GAME_DAY, TimeUpdater.getInstance().today());
		player.addStone(STONE_REWARD, Cause.SHARE_WEIBO);
		return null;
	}

	@Override
	public boolean isShared(Player player) {
		return player.getPool().getIntValue(PlayerPool.PROPERTY_LAST_SHARE_GAME_DAY) == TimeUpdater.getInstance().today();
	}

}
